﻿
@{
    ViewData["Title"] = "About";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>View</title>
</head>
<body>
    <div id="map" style="height:300px;">你好呀！</div>
</body>
</html>

<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=gY6PKbyHw0AaMB1bxpZSjOmQ"></script>
<script type="text/javascript">
    var map;
    var gpsPoint;
    var baiduPoint;
    var gpsAddress;
    var baiduAddress;
    var x;
    var y;
    function getLocation() {
        //根据IP获取城市
        var myCity = new BMap.LocalCity();
        myCity.get(getCityByIP);

        //获取GPS坐标
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 });
        } else {
            alert("您的浏览器不支持使用HTML 5来获取地理位置服务");
        }
    }

    function showMap(value) {
        var longitude = value.coords.longitude;
        var latitude = value.coords.latitude;
        map = new BMap.Map("map");
        x = latitude;
        y = longitude;
        alert("坐标经度为：" + latitude + "， 纬度为：" + longitude );
        gpsPoint = new BMap.Point(longitude, latitude); // 创建点坐标


        //根据坐标逆解析地址
        //var geoc = new BMap.Geocoder();
        //geoc.getLocation(gpsPoint, getCityByCoordinate);

        //BMap.Convertor.translate(gpsPoint, 0, translateCallback);
        //map.enableScrollWheelZoom(true);
        var convertor = new BMap.Convertor();
        var pointArr = [];
        pointArr.push(gpsPoint);
        convertor.translate(pointArr, 1, 5, translateCallback)

    }

    translateCallback = function (data) {
        var gg = data.points[0]
        alert("转换后坐标为："+gg.lng+","+gg.lat);
        map.centerAndZoom(data.points[0], 18);
        var rp = new BMap.Point(gg.lng, gg.lat);
        var geoc = new BMap.Geocoder();
        getAddress(data.points[0]);
        //geoc.getLocation(rp, getCityByBaiduCoordinate);
    }

    function getCityByCoordinate(rs) {
        gpsAddress = rs.addressComponents;
        var address = "GPS标注：" + gpsAddress.province + "," + gpsAddress.city + "," + gpsAddress.district + "," + gpsAddress.street + "," + gpsAddress.streetNumTber;
        alert(address);
        var marker = new BMap.Marker(gpsPoint); // 创建标注
        map.addOverlay(marker); // 将标注添加到地图中
        var labelgps = new BMap.Label(address, { offset: new BMap.Size(20, -10) });
        marker.setLabel(labelgps); //添加GPS标注
    }
    function getAddress(point) {
        var gc = new BMap.Geocoder();
        gc.getLocation(point, function (rs) {
            var addComp = rs.addressComponents;
            var poi = rs.surroundingPois[0];
            var addressOne = addComp.province + addComp.city + addComp.district;
            var address = addressOne + addComp.street + addComp.streetNumTber + " " + poi.title + "附近";//获取地址
            alert(address);
            
        });

    }
    function getCityByBaiduCoordinate(rs) {
        baiduAddress = rs.addressComponents;
        var address = "百度标注：" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumTber;
        alert(address);
        var marker = new BMap.Marker(rs); // 创建标注
        map.addOverlay(marker); // 将标注添加到地图中
        var label = new BMap.Label("转换后的百度坐标（正确）", { offset: new BMap.Size(20, -10) });
        marker.setLabel(label); //添加百度label
        map.setCenter(rs);
    }

    //根据IP获取城市
    function getCityByIP(rs) {
        var cityName = rs.name;
        alert("根据IP定位您所在的城市为:" + cityName);
    }

    function handleError(value) {
        switch (value.code) {
            case 1:
                alert("位置服务被拒绝");
                break;
            case 2:
                alert("暂时获取不到位置信息");
                break;
            case 3:
                alert("获取信息超时");
                break;
            case 4:
                alert("未知错误");
                break;
        }
    }

    function init() {
        getLocation();
    }

    window.onload = init;

</script>
